<template>
  <div>
    <vab-card>
      <template #header>
        <span class="leve_title">产品查询</span>
      </template>
      <template #default>
        <el-form
          :ref="(el) => (searchFormRef = el)"
          :model="searchForm"
          label-width="100px"
          label-position="left"
          class="form_search"
        >
          <el-row :gutter="24">
            <el-col
              v-for="(item, i) in InfoSearchFor"
              :key="i"
              :xs="24"
              :sm="12"
              :md="8"
              :lg="6"
              :xl="6"
            >
              <!-- 默认项 -->
              <el-form-item
                v-if="item.type == 'select'"
                :label="item.label"
                :prop="item.prop"
              >
                <el-select
                  clearable
                  class="width_100 height_auto"
                  collapse-tags
                  collapse-tags-tooltip
                  :multiple="item.multiple"
                  :filterable="item.multiple"
                  v-model.trim="searchForm[item.prop]"
                  @keyup.enter.native="onkeydown"
                >
                  <el-option
                    clearable
                    v-for="(jtem, j) in selectOptions(item.prop)"
                    :key="jtem.value"
                    :label="jtem.label"
                    :value="jtem.value"
                  />
                </el-select>
              </el-form-item>
              <el-form-item
                v-if="item.type == 'lazySelect'"
                :label="item.label"
                :prop="item.prop"
              >
                <ProductSelect v-model.trim="searchForm[item.prop]" :type="item.prop" />
              </el-form-item>
              <!-- 一级类目 -->
              <el-form-item
                v-if="item.type == 'select_1'"
                :label="item.label"
                :prop="item.prop"
              >
                <el-select
                  clearable
                  class="width_100 height_auto"
                  :multiple="item.multiple"
                  :filterable="item.multiple"
                  collapse-tags
                  collapse-tags-tooltip
                  v-model.trim="searchForm[item.prop]"
                  @keyup.enter.native="onkeydown"
                  @change="(value: string) => {
                    _ProductSearchClass(value)
                  }"
                >
                  <el-option
                    clearable
                    v-for="(jtem, j) in selectOptions(item.prop)"
                    :key="jtem.value"
                    :label="jtem.label"
                    :value="jtem.value"
                  />
                </el-select>
              </el-form-item>
              <!-- 二级类目 -->
              <el-form-item
                v-if="item.type == 'select_2'"
                :label="item.label"
                :prop="item.prop"
              >
                <el-select
                  clearable
                  class="width_100 height_auto"
                  collapse-tags
                  collapse-tags-tooltip
                  :multiple="item.multiple"
                  :filterable="item.multiple"
                  v-model.trim="searchForm[item.prop]"
                  @keyup.enter.native="onkeydown"
                >
                  <el-option
                    clearable
                    v-for="(jtem, j) in classTwoList"
                    :key="jtem.value"
                    :label="jtem.label"
                    :value="jtem.value"
                  />
                </el-select>
              </el-form-item>
            </el-col>

            <el-col :xs="24"
              :sm="12"
              :md="8"
              :lg="6"
              :xl="6">
              <div class="flex_just_end">
                <el-button size="small" type="primary" @click="handleSearch()">
                  查询
                </el-button>
                <el-button size="small" @click="resetForm(searchFormRef)">
                  重置
                </el-button>
              </div>
            </el-col>
          </el-row>
        </el-form>
      </template>
    </vab-card>
    <vab-card shadow="always">
      <template #header>
        <span class="leve2_title">产品明细列表</span>
      </template>
      <el-table
        border
        height="450"
        style="width: 100%"
        :data="data.dataSource"
        v-loading="data.loading"
      >
        <el-table-column label="序号" align="center" width="60" >
          <template #default="scope">
            <span
              v-text="(data.pageNum - 1) * data.pageSize + (scope.$index + 1)"
            ></span>
          </template>
        </el-table-column>
        <el-table-column
          prop="assistant_name"
          label="产品图"
          width="80"
          align="center"
        >
          <template #default="scope">
            <div v-if="scope.row.image && scope.row.image.length">
              <img class="preview_img" v-for="(item, i) in scope.row.image" :src="item.img_url" :key="i" alt="" @click="handlePreviewImg(item.img_url)">
            </div>
            <div v-else>
              --
            </div>
          </template>
        </el-table-column>
        <el-table-column
          prop="assistant_name"
          label="品牌"
          width="80"
          align="center"
        >
          <template #default="scope">
            <span class="ellipsis">
              {{ scope.row.assistant_name }}
            </span>
          </template>
        </el-table-column>
        <el-table-column
          prop="number"
          label="产品编码"
          width="160"
          align="center"
        >
          <template #default="scope">
            <span class="ellipsis">
              {{ scope.row.number }}
            </span>
          </template>
        </el-table-column>
        <el-table-column
          prop="name"
          label="产品名称"
          width="200"
          align="center"
        >
          <template #default="scope">
            <span class="link ellipsis" @click="handleLink(scope.row)">
              {{ scope.row.name }}
            </span>
          </template>
        </el-table-column>
        <el-table-column
          v-for="(item, i) in data.cloums"
          :key="i"
          :width="item.width"
          :prop="item.prop"
          :label="item.label"
          :align="item.type == 'money' || item.type == 'number' ? 'right' : 'center'"
        >
          <template #default="scope">
            <!-- 正常 -->
            <span 
              v-if="!item.type"
              class="ellipsis"
            >
              {{ _FieldFormat(scope.row[item.prop]) }}
            </span>
            <!-- 状态 -->
            <span 
              v-if="item.type == 'boolean'"
              class="ellipsis"
            >
              <el-tag v-if="scope.row[item.prop] == 1" class="ml-2" type="success">是</el-tag>
              <el-tag v-if="scope.row[item.prop] == 2" class="ml-2" type="warning">否</el-tag>
              <span v-if="scope.row[item.prop] == 0" class="ml-2" type="info">--</span>
            </span>
          </template>
        </el-table-column>
      </el-table>
      <div class="flex_just_end">
        <el-pagination
          background
          small="small"
          v-model:current-page="data.pageNum"
          v-model:page-size="data.pageSize"
          layout="total, prev, pager, next,sizes, jumper"
          :page-sizes="[20, 50, 100, 200]"
          :pager-count="10"
          :total="data.count"
          @current-change="handlePageNum"
          @size-change="handlePageSize"
        />
      </div>
    </vab-card>
    <VabPreviewImg :ref="(el) => vabPreviewImgRef = el"/>
  </div>
</template>

<script lang="ts" setup>
defineOptions({
  name: 'ProductInfo',
})
import { ElMessage, FormInstance } from 'element-plus'
/**
 * @exports
 */
import {
  InfoSearchFor,
  InfoData,
  InfoCloums,
  InfoSearchSelect,
} from './components/tools'
import { FieldFormat } from '~/src/utils/utils'
import ProductSelect from '/@/components/lazySelect/productSelect.vue'
import { getProductList, getProductSearch, productSearchClass } from '~/src/api/dms/product'
/**
 * @var
 */
const searchFormRef = ref<any>(null)
const vabPreviewImgRef = ref()
const router = useRouter()
let searchForm = reactive<any>({})
const data = reactive<any>({
  dataSource: [],
  cloums: [],
  pageNum: 1,
  pageSize: 20,
  count: 0,
  search: {},
})
data.dataSource = InfoData
data.cloums = InfoCloums
const radio = ref(0)

let classTwoList = ref<any>([])
/**
 * @method
 */
const handleSearch = async (value = '') => {
  await _GetProductList()
  ElMessage({
    message: '查询成功',
    type: 'success',
  })
}
const resetForm = async (formEl: FormInstance | undefined) => {
  if (!formEl) return
  await formEl.resetFields()
  searchForm = {}
  data.pageNum = 1
  data.pageSize = 20

  await _GetProductList()
  ElMessage({
    message: '重置成功',
    type: 'success',
  })
}
const handleLink = (row: any) => {
  router.push({
    path: '/product/detail',
    query: {
      id: row.id,
      type: 1,
      from: 'detail',
    }
  })
}
const onkeydown = (value: any) => {
  _GetProductList()
}

const handlePageNum = (value: any) => {
  data.pageNum = value
  _GetProductList()
}
const handlePageSize = (value: any) => {
  data.pageSize = value
  _GetProductList()
}
// 预览图片
const handlePreviewImg = (file = '') => {
  vabPreviewImgRef.value.open(file)
}

const selectOptions = (value: string) => {
  if (JSON.stringify(data.search) == '{}') return
  return InfoSearchSelect(data.search, value)
}

const createFilter = (queryString: string) => {
  return (restaurant: any) => {
    return (
      restaurant.value.toLowerCase().indexOf(queryString.toLowerCase()) === 0
    )
  }
}

const _FieldFormat = (value: string, type = '') => {
  return FieldFormat(value, type)
}
const _GetProductList = async () => {
  data.loading = true
  const query = {
    page: data.pageNum,
    limit: data.pageSize,
    type: 2, //固定必传，1带价格和编辑，2只查询
    filter: {
      assistant_id: searchForm.assistant_id || [], // 品牌
      name: searchForm.name || [], // 物料名称
      number: searchForm.number || [], // 物料编码
      barcode: searchForm.barcode || [], // 条码
      class_id1: searchForm.class_id1 || [], // 一级类目
      class_id2: searchForm.class_id2 || [], // 二级类目
      xl_id: searchForm.xl_id || [], // 系列
      large_category_id: searchForm.large_category_id || [], // 大类
      is_master: searchForm.is_master || [], // 是否主推
      is_examine: searchForm.is_examine || [], // 是否考核
      is_img: searchForm.is_img || ''
    },
  }
  try {
    const res = await getProductList({ ...query })
    data.dataSource = res.data.list
    data.count = res.data.count
    data.loading = false
  } catch (error) { }
}

const _GetProductSearch = async () => {
  classTwoList.value = []
  try {
    const res = await getProductSearch()

    res.data.brand.push({ entry_id: '', name: '空' })
    res.data.class_one.push({ id: '', data_title: '空' })
    res.data.class_two.push({ id: '', data_title: '空' })
    res.data.series.push({ id: '', data_title: '空' })
    res.data.category.push({ id: '', data_title: '空' })
    data.search = await res.data
    classTwoList.value = selectOptions('class_id2')

  } catch (error) { }
}
// 二级分类
const _ProductSearchClass = async (class_id: any) => {
  try {
    const res = await productSearchClass({ class_id })
    classTwoList = res.data.map((item: any) => {
      return {
        value: item.id,
        label: item.data_title
      }
    })
  } catch (error) {

  }
}

onMounted(() => {
  _GetProductList()
  _GetProductSearch()
})
</script>

<style lang="scss" scoped>
@import '../../components/public.scss';
</style>
